<?php
class Home_Model_Bankroll extends Home_Model_Base {

    protected $sqls = array(
        'get_list'  => 'select * from `fi_bankroll` {{@where}} order by {{@order_by}} limit {{@limit}};',
        'get_all_count_validity_date' => 'select count(1) from `fi_bankroll` where `bankroll_validity_date`>[@now];',
    );
    
    public function __construct(){
        parent::__construct();
    }

    public $_bankroll_category = array(
        '0'     => array('title'=> '不限'),
        'b10'   => array(
            'title' => '非金融机构资金',
            'info'  => '发布方希望寻找一手项目，以多种出资方式直接对其进行投资的资本信息。比如：风投基金，收并购需求',
        ),
        'b20'   => array(
            'title' => '民间资金',
            'info'  => '发布方拥有资金可做短期拆借，希望寻找相应的交易对手的信息比如：过桥资金，银行间拆借资金等信息',
        ),
        'b30'   => array(
            'title' => '金融机构资金',
            'info'  => '发布方拥有正规金融机构的资金来源，希望寻找有标准化融资需求的企业的信息。比如：贷款产品，担保融资产品等',
        )
    );

    public $_bankroll_area = array(
        '0' => '不限',
        '1' => '北京',
        '2' => '上海',
        '3' => '广州',
        '4' => '深圳',
        '5' => '成都',
        '6' => '宁波',
        '7' => '香港',
        '8' => '澳门',
    );

    public function get_list($condition = array(), $page = 1, $page_size = 10, $order_by = 'updated desc'){
        $page_index = ($page>=1)?($page-1)*$page_size:0;
        $whereArr = array();
        if(empty($condition))
            $sql = str_replace('{{@where}}',' WHERE `bankroll_validity_date`>'.time(),$this->sqls['get_list']);
        else{
            $condition['project_validity_date'] = array('symbol'=>'>','value'=>time());
            $whereStr = $this->_where($condition, $whereArr);
            $sql = str_replace('{{@where}}',$whereStr,$this->sqls['get_list']);
        }

        $sql = str_replace('{{@order_by}}',$order_by,$sql);
        $sql = str_replace('{{@limit}}',$page_index.','.$page_size,$sql);
        return mysql::fetch('bankroll', $sql, $whereArr);
    }

    //获取所有记录数
    public function get_all_count_validity_date(){
        $now = time();
        $c = mysql::fetch('bankroll', $this->sqls['get_all_count_validity_date'],array('now'=>$now));
        return $c[0]['count(1)'];
    }
}